﻿/*****************************************介绍*****************************************
 * 作者：24531
 * 创建时间：2025-09-04 21:50:02
 * 功能：界面【ItemPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using Game.UI.GamePanel;
using UnityEngine.UI;

namespace Game.UI
{
    public abstract class ItemPanelView : PopupPanel
    {
        protected int m_NeedCoin;
        protected GamePanelView m_GameView;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(GetPanelKey(), this);
            EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_CLOSE_AD, OnCloseAd);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(GetPanelKey());
            EventCenter.Instance.RemoveEventListen<EAdType, ETriggerAdType>(GameEventName.ON_CLOSE_AD, OnCloseAd);
        }

        public override void Init(object data)
        {
            m_GameView = data as GamePanelView;
            m_GameView.PauseGame();
        }

        protected override void OnEnable()
        {
            GetControl<Text>(ItemPanelNotifications.CoinNum_CTRL).text = GetNeedCoins().ToString();
            base.OnEnable();
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case ItemPanelNotifications.CloseBtn_CTRL:
                    CloseBtnHandle();
                    break;
                case ItemPanelNotifications.CoinBtn_CTRL:
                    if (GameDataMgr.Instance.TotalCoinsNum < GetNeedCoins())
                        UIUtility.ShowToast(BaseDataMgr.Instance.GetLanguageValue("The remaining gold coins are insufficient!!"));
                    else
                    {
                        // 扣除金币
                        GameDataMgr.Instance.UpdateCoins(-GetNeedCoins());
                        OnCloseAd(EAdType.RewardedVideo, GetAdTriggerType()); // 发放成功奖励
                    }
                    break;
                case ItemPanelNotifications.ClaimBtn_CTRL:
                    NativeInteraction.Instance.ShowRewardAd(GetAdTriggerType());
                    break;
            }
        }

        protected virtual void CloseBtnHandle()
        {
            UIUtility.HidePanel(GetPanelKey());
            if (this is not LevelFailurePanel.LevelFailurePanelView)
                m_GameView.ResumeGame();
        }

        private void OnCloseAd(EAdType adType, ETriggerAdType triggerType)
        {
            if (adType != EAdType.RewardedVideo || triggerType != GetAdTriggerType()) return;
            DistributeAdRewards();
            UIUtility.HidePanel(GetPanelKey());
        }

        protected abstract ETriggerAdType GetAdTriggerType();

        protected abstract string GetPanelKey();

        /// <summary>发放广告奖励（只发放奖励，不进行额外操作）</summary>
        protected abstract void DistributeAdRewards();

        // 获取需要消耗的金币
        protected abstract int GetNeedCoins();
    }
}